<?php
namespace Api\Traits;

use Member\Model\MemberCompanyModel;
use Think\Log;

/**
 * author: Administrator
 * Createtime: 2015/10/2615:20
 * description: 微信相关接口
 */

trait Weixin
{
    /*
     *
     * */
    public function ces()
    {
        $this->display();
    }

    /**
     *用户注册
     */
    public function doRegiser()
    {
        $this->validateToken();
        $this->memberConfig = cache("Member_Config");
        if (empty($this->memberConfig['allowregister'])) {
            $this->message(['info' => '系统不允许新会员注册'], array(), 'error');
        }
        $this->memberDb = D('Member/Member');

        $loginName = I('request.username', null, 'trim');//用户名
        $password = trim(I('request.password'));//密码
        $modelid = I('request.modelid');
        if(empty($modelid)){
            $this->message(['info' => '请输入模型ID'], array(), 'error');
        }

        if (empty($loginName)) {
            $this->message(['info' => '请输入您的手机号码'], array(), 'error');
        }
        if (!isMobile($loginName)) {
            $this->message(['info' => '手机号码格式错误'], array(), 'error');
        }
        $loginNamestatus = service("Passport")->userCheckUsername($loginName);
        if (!$loginNamestatus) {
            $this->message(['info' => '手机号码已经存在'], array(), 'error');
        }


        if (empty($password)) {
            $this->message(20024, array(), 'error');
        }
        if (false == isMin($password, 6)) {
            $this->message(['info' => '密码不能小于6位'], array(), 'error');
        }
        $this->userInfo['username'] = $loginName;
        $this->userInfo['password'] = $password;
        $this->userInfo['email'] = I('request.email') ? I('request.email') : $loginName . '@stjy.com';

        if ($modelid == 4) {//企业用户注册信息

            $this->memberInfo['f_name'] = trim(I('request.f_name'));//企业名称;
            $this->memberInfo['f_scale'] = trim(I('request.f_scale'));//企业规模;
            $this->memberInfo['f_nayure'] = trim(I('request.f_nayure')); //企业性质
            $this->memberInfo['f_setupdate'] = trim(I('request.f_setupdate')); //企业注册时间
            $this->memberInfo['f_money'] = trim(I('request.f_money')); //企业注册资金
            $this->memberInfo['f_regionID'] = trim(I('request.f_regionID')); //企业注册地ID
            $this->memberInfo['f_abstract'] = trim(I('request.f_abstract')); //企业简介
            $this->memberInfo['f_license'] = trim(I('request.f_license')); //企业营业执照
            $this->memberInfo['f_firmperson'] = trim(I('request.f_firmperson')); //企业联系人姓名
            $this->memberInfo['f_phone'] = trim(I('request.f_phone')); //企业联系人电话
            $this->memberInfo['f_location'] = trim(I('request.f_location')); //企业办公地点

            $menmberCompayModel = new MemberCompanyModel();
            if (!$menmberCompayModel->create($this->memberInfo)) {
                $this->message(['info' => $menmberCompayModel->getError()], array(), 'error');
            }

        }

        $info = $this->memberDb->token(false)->create($this->userInfo); //手动create 使得$_auto生效
        $info['modelid'] = $modelid;
        $userid = service("Passport")->userRegister($this->userInfo['username'], $this->userInfo['password'], $this->userInfo['email']);
        if ($userid > 0) {
            $memberinfo = service("Passport")->getLocalUser((int)$userid);
            $info['username'] = $memberinfo['username'];
            $info['password'] = $memberinfo['password'];
            $info['email'] = $memberinfo['email'];
            $info['userid'] = $userid;
            //写入模型关联信息表数据
            $info['modelid'] = $modelid;
            $ContentModel = \Content\Model\ContentModel::getInstance($info['modelid']);
            $content_input = new \content_input($info['modelid']);
            $inputinfo = $content_input->get($this->memberInfo, 3);
            if ($inputinfo) {
                //数据验证
                $inputinfo = $ContentModel->token(false)->create($inputinfo, 2);
                if (false == $inputinfo) {
                    $ContentModel->tokenRecovery($this->memberInfo);
                    $this->message(['info' => $ContentModel->getError()], array(), 'error');
                }
                $inputinfo['userid'] = $userid;

                $adddata = array_filter($inputinfo);
                $status = $ContentModel->add($adddata);
            }
            if ($modelid == 5) {
                $info['checked'] = 1; //个人用户不用审核
            }
            if (false !== $this->memberDb->where(array('userid' => $memberinfo['userid']))->save($info)) {

                unset($info['password']);

                //进行微信用户绑定
                $connect_openid = I('post.openid');
                $connect_app = I('post.app');
                if ($connect_openid && $connect_app == 'weixin') {
                    $connectdata = array(
                        'openid' => $connect_openid,
                        'uid' => $userid,
                        'app' => $connect_app,
                        'accesstoken' => $connect_openid,
                        'expires' => time(),
                    );
                    $Connect = D('Member/Connect');
                    $Connect->connectAdd($connectdata); //绑定
                }

                $this->message("注册成功！", [
                    'error' => 10000,
                    'UserInfo' => array_merge($info, $inputinfo),
                ]);
            } else {
                //删除
                service("Passport")->userDelete($memberinfo['userid']);
                $this->message(['info' => '会员注册失败'], [], 'error');
            }

        } else {
            $this->message(['info' => service("Passport")->getError() ?: '注册失败'], array(), 'error');
        }

    }


    /**
     *简历创建
     */
    public function addresume()
    {
        header("Content-type: text/html; charset=utf-8");
        if (IS_POST) {
            $userid = I('post.userid');
            $onset = I('request.onset');
            $persoangeModel = M('member_personage');

            if($onset =='title'){  //修改标题
                $rules = array(
                    array('title','require','标题必须！'), //默认情况下用正则进行验证
                );
            }elseif($onset =='datum'){ //基本资料
                $rules = array(
                    array('p_name','require','真实姓名必须！'), //默认情况下用正则进行验证
                    array('p_sex','require','性别必须！'), //默认情况下用正则进行验证
                    array('p_birthday','require','出生日期必须！'), //默认情况下用正则进行验证
                    array('p_nation','require','民族必须！'), //默认情况下用正则进行验证
                    array('p_politicsstatus','require','政治面貌必须！'), //默认情况下用正则进行验证
                    array('p_marriage','require','婚姻状况必须！'), //默认情况下用正则进行验证
                    array('p_height','require','身高必须！'), //默认情况下用正则进行验证
                    array('p_vision','require','视力必须！'), //默认情况下用正则进行验证
                    array('p_newpay','require','目前薪资必须！'), //默认情况下用正则进行验证
                    array('p_phone','require','联系电话必须！'), //默认情况下用正则进行验证
                    array('p_censusregister','require','户籍所在地必须！'), //默认情况下用正则进行验证
                    array('p_nowlocation','require','目前所在地必须！'), //默认情况下用正则进行验证
                    array('p_drive','require','驾照等级必须！'), //默认情况下用正则进行验证
                    array('p_jobdate','require','工作经验必须！'), //默认情况下用正则进行验证
                 /*   array('name','','帐号名称已经存在！',0,'unique',1), // 在新增的时候验证name字段是否唯一
                    array('value',array(1,2,3),'值的范围不正确！',2,'in'), // 当值不为空的时候判断是否在一个范围内
                    array('repassword','password','确认密码不正确',0,'confirm'), // 验证确认密码是否和密码一致
                    array('password','checkPwd','密码格式不正确',0,'function'), // 自定义函数验证密码格式*/
                );
            }elseif($onset =='apply'){ //求职意向
                $rules = array(
                    array('p_job_type','require','求职意向必须！'), //默认情况下用正则进行验证
                    array('p_job_post','require','应聘职位必须！'), //默认情况下用正则进行验证
                    array('p_payscope','require','薪资待遇必须！'), //默认情况下用正则进行验证
                    array('p_workplace','require','意向工作地点必须！'), //默认情况下用正则进行验证
                    array('p_stay','require','住宿要求必须！'), //默认情况下用正则进行验证
                    array('p_comedate','require','到岗时间必须！'), //默认情况下用正则进行验证
                   // array('p_other','require','其它要求必须！'), //默认情况下用正则进行验证
                );
            }elseif($onset =='work'){ //工作经历
                $data['start'] = I('post.start'); //开始时间
                $data['end'] = I('post.end');      //结束时间
                $data['companyname'] = I('post.companyname'); //公司名称
                $data['postname'] = I('post.postname'); //职位名称
            }elseif($onset =='education'){//教育背景
                $data['start'] = I('post.start'); //开始时间
                $data['end'] = I('post.end');      //结束时间
                $data['shoolname'] = I('post.shoolname'); //学校名称
                $data['education'] = I('post.education'); //学历
            }
            if ($persoangeModel->where(['userid' => $userid])->find()) {  //判断该用户是否有简历存在
                $save = 'save';
                $persoangeModel->updatedate = time();
            } else {
                $save = 'add';
                $persoangeModel->islock = 1;
                $persoangeModel->createdate = time();
                $persoangeModel->updatedate = time();
            }
            if($onset =='work'){
                $list = $persoangeModel->where(['userid' => $userid])->field('p_job_undergo')->find();
                $array = unserialize($list['p_job_undergo']);
                if($array){
                    $i = 1;
                    foreach($array as $k=>$v){//新增时从新排列id
                        $array[$k]['id'] = $i;
                        $i++;
                    }
                    $data['id'] = (count($array)+1);
                }else{
                    $data['id'] = 1;
                }
                $array[] = $data;
                $arr['p_job_undergo'] = serialize($array); //把工作经验的数组序列化
            }elseif($onset =='education'){
                $list = $persoangeModel->where(['userid' => $userid])->field('p_education_undergo')->find();
                $array = unserialize($list['p_education_undergo']);
                if($array){
                    $i = 1;
                    foreach($array as $k=>$v){  //新增时从新排列id
                        $array[$k]['id'] = $i;
                        $i++;
                    }
                    $data['id'] = (count($array)+1);
                }else{
                    $data['id'] = 1;
                }
                $array[] = $data;
                $arr['p_education_undergo'] = serialize($array);  //把学习经历数组序列化
            }

            if (!$persoangeModel->validate($rules)->create()){     // 如果创建失败 表示验证没有通过 输出错误提示信息
                     $this->message(['info' => $persoangeModel->getError()], array(), 'error');
            }else{     // 验证通过 可以进行其他数据操作

                if ($persoangeModel->where(['userid' => $userid])->$save($arr)) {
                    $this->message("操作成功！", ['error' => 10000]);
                } else {
                    $this->message(['info' => '没有任何修改'], array(), 'error');
                }

            }
        }
    }


    /**
     *  简历 -- 工作经历及教育经历删除
     */
    public function remove()
    {
        $userid = I('post.userid',0,'intval');
        $onset  = I('post.onset');
        $id = I('post.id',0,'intval');
        $persoangeModel = M('member_personage');
        if($onset =='work'){
            $list = $persoangeModel->where(['userid' => $userid])->field('p_job_undergo')->find();
            $array = unserialize($list['p_job_undergo']);
            if(is_array($array)){
                foreach ($array as $k=>$v){
                    if($v['id'] == $id){
                        unset($array[$k]);
                    }
                }
            }
            $arr['p_job_undergo'] = serialize($array); //把工作经验的数组序列化
        }elseif($onset =='education'){
            $list = $persoangeModel->where(['userid' => $userid])->field('p_education_undergo')->find();
            $array = unserialize($list['p_education_undergo']);
            if(is_array($array)){
                foreach ($array as $k=>$v){
                    if($v['id'] == $id){
                        unset($array[$k]);
                    }
                }
            }
            $arr['p_education_undergo'] = serialize($array);  //把学习经历数组序列化
        }

        if ($persoangeModel->where(['userid' => $userid])->save($arr)) {
            $this->message("删除成功！", ['error' => 10000]);
        } else {
            $this->message(['info' => '删除失败！'], array(), 'error');
        }
    }

    /**
     *  简历 -- 工作经历及教育经历修改
     */
    public function savejob()
    {
        $userid = I('post.userid',0,'intval'); //用户id
        $onset  = I('post.onset');
        $persoangeModel = M('member_personage');
        if($onset =='work'){ //工作经历
            $data['id'] = I('post.id',0,'intval'); //工作经历ID
            $data['start'] = I('post.start'); //开始时间
            $data['end'] = I('post.end');      //结束时间
            $data['companyname'] = I('post.companyname'); //公司名称
            $data['postname'] = I('post.postname'); //职位名称
        }elseif($onset =='education'){//教育背景
            $data['id'] = I('post.id',0,'intval'); //教育经历ID
            $data['start'] = I('post.start'); //开始时间
            $data['end'] = I('post.end');      //结束时间
            $data['shoolname'] = I('post.shoolname'); //学校名称
            $data['education'] = I('post.education'); //学历
        }
        if($onset =='work'){
            $list = $persoangeModel->where(['userid' => $userid])->field('p_job_undergo')->find();
            $array = unserialize($list['p_job_undergo']);
            if(is_array($array)){
                foreach ($array as $k=>$v){
                    if($v['id'] == $data['id']){
                        $array[$k]['start'] = $data['start'];
                        $array[$k]['end'] = $data['end'];
                        $array[$k]['companyname'] = $data['companyname'];
                        $array[$k]['postname'] = $data['postname'];
                    }
                }
            }
            $arr['p_job_undergo'] = serialize($array); //把工作经验的数组序列化
        }elseif($onset =='education'){
            $list = $persoangeModel->where(['userid' => $userid])->field('p_education_undergo')->find();
            $array = unserialize($list['p_education_undergo']);
            if(is_array($array)){
                foreach ($array as $k=>$v){
                    if($v['id'] == $data['id']){
                        $array[$k]['start'] = $data['start'];
                        $array[$k]['end'] = $data['end'];
                        $array[$k]['shoolname'] = $data['shoolname'];
                        $array[$k]['education'] = $data['education'];
                    }
                }
            }
            $arr['p_education_undergo'] = serialize($array);  //把学习经历数组序列化
        }

        if ($persoangeModel->where(['userid' => $userid])->save($arr)) {
            $this->message("修改成功！", ['error' => 10000]);
        } else {
            $this->message(['info' => '修改失败！'], array(), 'error');
        }
    }


    /**
     *      简历  工作经历、教育经历查看
     */
    public function joblock()
    {
        $userid = I('post.userid',0,'intval'); //用户id
        $onset  = I('post.onset');
        $id = I('post.id',0,'intval'); //工作经历ID
        $persoangeModel = M('member_personage');
        $list = $persoangeModel->where(['userid'=>$userid])->field('p_education_undergo,p_job_undergo')->find();
        $data = array();

        if($onset == 'work'){
            $array = unserialize($list['p_job_undergo']);
            foreach($array as $k=>$v){
                if($id==$v['id']){
                    $data = $v;
                }
            }
        }elseif($onset == 'education'){
            $array = unserialize($list['p_education_undergo']);
            foreach($array as $k=>$v){
                if($id==$v['id']){
                    $data = $v;
                }
            }
        }
        $this->message("", [
            'error' => 10000,
            'data' => $data,
        ]);

    }


    /**
     *  分类调用及职位调用
     * job_comedate  到岗时间
     * job_seeing 视力情况
     * job_politics 政治面貌
     * job_drive 驾照等级
     * job_scale 企业规模
     * job_experience  工作经验
     * job_education  学历
     * job_jobs_nature  工作性质
     * job_wage 薪资待遇
     * job_driver_age 驾龄要求
     * category_jobs  职位名称
     */
    public function returncolumn()
    {
        header("Content-type: text/html; charset=utf-8");
        $type = S('classify');
        if(!$type) {
            $array = ['job_comedate', 'job_seeing', 'job_politics', 'job_drive', 'job_scale', 'job_experience', 'job_education', 'job_jobs_nature', 'job_wage', 'job_driver_age'];
            $list = M('category_menu')->where(['c_alias' => ['in', $array]])->field('c_id,c_name,c_alias')->select();//查询分类
            $list2 = M('category_jobs')->where(['parentid' => 528])->field('id,categoryname')->select(); //查询职位
            foreach ($list as $v) {
                if ($v['c_alias'] == 'job_comedate') {
                    $data['job_comedate'][] = $v;
                } elseif ($v['c_alias'] == 'job_seeing') {
                    $data['job_seeing'][] = $v;
                } elseif ($v['c_alias'] == 'job_politics') {
                    $data['job_politics'][] = $v;
                } elseif ($v['c_alias'] == 'job_drive') {
                    $data['job_drive'][] = $v;
                } elseif ($v['c_alias'] == 'job_scale') {
                    $data['job_scale'][] = $v;
                } elseif ($v['c_alias'] == 'job_experience') {
                    $data['job_experience'][] = $v;
                } elseif ($v['c_alias'] == 'job_education') {
                    $data['job_education'][] = $v;
                } elseif ($v['c_alias'] == 'job_jobs_nature') {
                    $data['job_jobs_nature'][] = $v;
                } elseif ($v['c_alias'] == 'job_wage') {
                    $data['job_wage'][] = $v;
                } elseif ($v['c_alias'] == 'job_driver_age') {
                    $data['job_driver_age'][] = $v;
                }
            }
            $data['category_jobs'] = $list2;
            S('classify', $data);
        }else{
            $data = $type;
        }
        $this->message("", [
            'error' => 10000,
            'data' => $data,
        ]);
    }

    /**
     *   简历完成进度
     */
    public function complete()
    {
        header("Content-type: text/html; charset=utf-8");
        $userid = I('post.userid',0,'intval');
        $persoangeModel = M('member_personage');
        $list['ziliao'] = 0;
        $list['jobtype'] = 0;
        $list['work'] = 0;
        $list['education'] = 0;
        $data = $persoangeModel->where(['userid'=>$userid])->find();
        if(!empty($data['p_name'])&&!empty($data['p_sex'])&&!empty($data['p_birthday'])&&!empty($data['p_nation'])&&!empty($data['p_politicsstatus'])&&!empty($data['p_marriage'])&&!empty($data['p_nowlocation'])&&!empty($data['p_censusregister'])&&!empty($data['p_phone'])&&!empty($data['p_newpay'])&&!empty($data['p_height'])&&!empty($data['p_vision']))
        { //基本资料
            $list['ziliao'] = 1;
        }
        if(!empty($data['p_job_post'])&&!empty($data['p_job_type'])&&!empty($data['p_payscope'])&&!empty($data['p_workplace'])&&!empty($data['p_comedate'])&&!empty($data['p_other']))
        {//求职意向
            $list['jobtype'] = 1;
        }
        if(unserialize($data['p_job_undergo']))
        {//工作经历
            $list['work'] = 1;
        }
        if(unserialize($data['p_education_undergo']))
        {//教育经历
            $list['education'] = 1;
        }
        $list['title'] = $data[title];
        $list['islock'] = $data[islock];
        $this->message("", [
            'error' => 10000,
            'data' => $list,
        ]);
    }

    /**
     *      简历公开修改
     */
    public function isopen()
    {
        $data['islock'] = I('post.islock')==1?2:1;

        $userid = I('post.userid');
        if(M('member_personage')->where(['userid'=>$userid])->save($data))
        {
            $this->message("操作成功！", ['error' => 10000]);
        }else{
            $this->message(['info' => '没有任何修改'], array(), 'error');
        }
    }

    /**
     *      简历刷新
     */
    public function renovation()
    {
        $id = I('post.userid');
        $data['updatedate'] = time();
        if(M('member_personage')->where(['userid'=>$id])->save($data)){
            $this->message("刷新成功！", ['error' => 10000]);
        }else{
            $this->message(['info' => '刷新失败'], array(), 'error');
        }
    }






















}